package com.tool.frame.model;

import com.tool.util.DbUtil;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

public class MyTableModel extends AbstractTableModel
{
	
	private static final long serialVersionUID = 2460028098250184972L;
	
	private final String[] COL_NAMES = { "选择", "表名", "包名", "生成服务层代码", "主键自动生成" };
	
	private String[] tableNames = null;
	
	private String[] packageNames = null;
	
	private Boolean[] checkBoxs = null;
	
	private Boolean[] selectedBoxs = null;
	
	private Boolean[] autoGenIds = null;
	
	public MyTableModel(String ds)
	{
		Connection conn = null;
		try
		{
			conn = DbUtil.getConnection(ds);
			this.tableNames = ((String[]) DbUtil.getTableNames(conn).toArray(
					new String[0]));
			this.packageNames = new String[this.tableNames.length];
			this.checkBoxs = new Boolean[this.tableNames.length];
			this.selectedBoxs = new Boolean[this.tableNames.length];
			this.autoGenIds = new Boolean[this.tableNames.length];
			for (int i = 0; i < this.tableNames.length; i++)
			{
				String tableName = this.tableNames[i];
				this.autoGenIds[i] = Boolean.valueOf(DbUtil.isIdAutoIncrement(
						conn, tableName));
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			
			if (conn != null)
			{
				try
				{
					conn.close();
				}
				catch (Exception e2)
				{
					e2.printStackTrace();
				}
			}
		}
		finally
		{
			if (conn != null)
			{
				try
				{
					conn.close();
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		}
	}
	
	public List getSelected()
	{
		List list = new ArrayList();
		for (int i = 0; i < this.selectedBoxs.length; i++)
		{
			if ((this.selectedBoxs[i] != null)
					&& (this.selectedBoxs[i].booleanValue()))
			{
				String tableName = this.tableNames[i];
				
				String packageName = this.packageNames[i] == null ? ""
						: this.packageNames[i];
				
				Boolean checkBoxs = this.checkBoxs[i];
				String checkBox = (null == checkBoxs) ? "0" : (checkBoxs
						.booleanValue() ? "1" : this.checkBoxs[i] == null ? "0"
						: "0");
				
				Boolean autoGenIds = this.autoGenIds[i];
				String autoGenId = null == autoGenIds ? "0" : autoGenIds
						.booleanValue() ? "1"
						: this.autoGenIds[i] == null ? "0" : "0";
				
				list.add(i + "," + tableName + "," + packageName + ","
						+ checkBox + "," + autoGenId);
			}
		}
		return list;
	}
	
	public String getColumnName(int column)
	{
		return this.COL_NAMES[column];
	}
	
	public int getColumnCount()
	{
		return this.COL_NAMES.length;
	}
	
	public int getRowCount()
	{
		return this.tableNames.length;
	}
	
	public Object getValueAt(int rowIndex, int columnIndex)
	{
		switch (columnIndex)
		{
		case 0:
			return this.selectedBoxs[rowIndex];
		case 1:
			return this.tableNames[rowIndex];
		case 2:
			return this.packageNames[rowIndex];
		case 3:
			return this.checkBoxs[rowIndex];
		case 4:
			return this.autoGenIds[rowIndex];
		}
		return null;
	}
	
	public boolean isCellEditable(int rowIndex, int columnIndex)
	{
		if (columnIndex == 1)
			return false;
		if (columnIndex == 4)
			return false;
		return true;
	}
	
	public void setValueAt(Object value, int rowIndex, int columnIndex)
	{
		switch (columnIndex)
		{
		case 0:
			this.selectedBoxs[rowIndex] = ((Boolean) value);
			break;
		case 2:
			this.packageNames[rowIndex] = ((String) value);
			break;
		case 3:
			this.checkBoxs[rowIndex] = ((Boolean) value);
			break;
		case 4:
			this.autoGenIds[rowIndex] = ((Boolean) value);
		case 1:
		}
	}
	
	public Class getColumnClass(int columnIndex)
	{
		if ((columnIndex == 3) || (columnIndex == 0) || (columnIndex == 4))
		{
			return Boolean.class;
		}
		return super.getColumnClass(columnIndex);
	}
}
